package spring;

import org.apache.commons.lang.StringUtils;
import spring.annotation.Component;

import java.lang.annotation.Annotation;

/**
 * @ProjectName: Spring
 * @Package: spring
 * @ClassName: AnnotationBeanNameGenerator
 * @Author: zxh
 * @Description: 用于生成注解编程的BeanName命名
 * @Date: 2022/3/10 14:44
 * @Version: 1.0
 */
public class AnnotationBeanNameGenerator {

    public String generateBeanName(Class beanClass){
        if(!beanClass.isAnnotationPresent(Component.class)){
            throw new NullPointerException();
        }
        Component component = (Component)beanClass.getAnnotation(Component.class);
        return StringUtils.isEmpty(component.value()) ? buildDefaultBeanName(beanClass) : component.value();
    }

    public static String buildDefaultBeanName(Class beanClass){
        String className = beanClass.getName();
        String[] split = className.split("\\.");
        String tempBeanName = split[split.length-1];
        char[] tempBeanNames = tempBeanName.toCharArray();
        tempBeanNames[0] += 32;
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < tempBeanNames.length; i++) {
            stringBuilder.append(tempBeanNames[i]);
        }
        return stringBuilder.toString();
    }

}
